package com.ys.controller.api.eKnowAi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.entity.eKnowAi.EKnowKnowledgeList;
import com.ys.entity.eKnowAi.EKnowKnowledgeType;
import com.ys.entity.eKnowAi.EKnowTimerSet;
import com.ys.enume.eKnowAi.EknowCodeEnum;
import com.ys.service.eKnowAi.EKnowKnowledgeTypeService;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.LayuiTableVoJA;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/inter/knowledgeType")
public class ApiInterKnowledgeTypeController {
	private static final Logger logger = LoggerFactory.getLogger(ApiInterKnowledgeTypeController.class);


	@Autowired
	private EKnowKnowledgeTypeService eKnowKnowledgeTypeService;


	/**
	 * 查询
	 */
	//@RequiresPermissions("eKnowKnowledgeList:view")
	@RequestMapping("/searchType")
	@ResponseBody
	public LayuiTableVoJA searchType(Model model, HttpServletRequest request, HttpServletResponse response,
									 String id
	){
		try {
			List<EKnowKnowledgeType> resList = new EKnowKnowledgeType()
					.setBaseId(Long.parseLong(id))
					.setOrderby("A.Sort ASC")
					.queryList();
			LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
			JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(resList));
			tableVoJA.setData(jsonArray);
			tableVoJA.setMsg("成功");
			tableVoJA.setCode(0);
			return tableVoJA;
		}catch (Exception e){
			logger.error("查询出错：",e.getMessage(),e);
			LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
			tableVoJA.setCode(1);
			tableVoJA.setMsg("查询出错，请稍后重试！");
			return tableVoJA;
		}
	}

	/**
	 * 新增、修改知识库分类信息
	 */
	@PostMapping("/addOrUpdateKnowledgeType")
	@ResponseBody
	public JSONObject addOrUpdateKnowledgeType(HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		JSONObject result = new JSONObject();
		try {

			String loginUserId = ShiroUtils.getUserId();//登录的用户ID
			long baseId = Long.parseLong(params.get("baseId").toString());//知识库Id
			String typeName = params.get("typeName").toString();//所属分类名称
			int sort = Integer.parseInt(params.get("sort").toString());//排序

			//入参
			EKnowKnowledgeType eKnowKnowledgeType = new EKnowKnowledgeType();
			eKnowKnowledgeType.setBaseId(baseId);
			eKnowKnowledgeType.setTypeName(typeName);
			eKnowKnowledgeType.setSort(sort);
			eKnowKnowledgeType.setUpdateUserId(loginUserId);
			int isSave = 0;

			if(params.get("addOrUpdate").toString().equals("add")) {//新增
				eKnowKnowledgeType.setStatus(0);
				isSave = eKnowKnowledgeTypeService.addKnowledgeType(eKnowKnowledgeType);
			}else {//修改
				eKnowKnowledgeType.setStatus(1);
				eKnowKnowledgeType.setId(Long.parseLong(params.get("id").toString()));
				isSave = eKnowKnowledgeTypeService.updateKnowledgeType(eKnowKnowledgeType);
			}
			if(isSave>0){
				result.put("code", EknowCodeEnum.ApiRetuenCode.SUCCESS);
			}else {
				result.put("code", EknowCodeEnum.ApiRetuenCode.FAIL);
			}
		}catch (Exception e) {
			result.put("code", EknowCodeEnum.ApiRetuenCode.FAIL);
			logger.error("逸知道新增、修改知识库分类信息出错：",e.getMessage(),e);
		}
		return result;
	}



	/**
	 * 删除知识库分类信息
	 */
	@PostMapping("/delKnowledgeType")
	@ResponseBody
	public JSONObject delKnowledgeType(HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		JSONObject result = new JSONObject();
		try {

			long id = Long.parseLong(params.get("id").toString());//Id
			//判断资料是否为空
			List<EKnowKnowledgeList> hasList = new EKnowKnowledgeList().setTypeId(id).queryList();
			if(hasList!=null && hasList.size()>0){
				result.put("code", EknowCodeEnum.ApiRetuenCode.CANNOT);
				return result;
			}
			//判断资料是否绑定定时器
			List<EKnowTimerSet> hasList2 = new EKnowTimerSet().setTypeId(id).queryList();
			if(hasList2!=null && hasList2.size()>0){
				result.put("code", EknowCodeEnum.ApiRetuenCode.HASTIMERSETNODEL);
				return result;
			}

			int isDel = eKnowKnowledgeTypeService.deleteKnowledgeType(id);
			if(isDel>0){
				result.put("code", EknowCodeEnum.ApiRetuenCode.SUCCESS);
			}else {
				result.put("code", EknowCodeEnum.ApiRetuenCode.FAIL);
			}
		}catch (Exception e) {
			result.put("code", EknowCodeEnum.ApiRetuenCode.FAIL.value);
			logger.error("逸知道删除知识库分类信息出错：",e.getMessage(),e);
		}
		return result;


	}



}